﻿using Logistics.EntityFrameworkCore;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.Contract
{
    public class CargoownerContractAppService : ApplicationService, ICargoownerContractAppService
    {
        private readonly IRepository<Cargoowner, int> _cargoownerRepository;
        private readonly ICargoownerContractRepository _cargoowner;
        private readonly LogisticsDbContext db;
        public CargoownerContractAppService(IRepository<Cargoowner, int> cargoownerRepository, ICargoownerContractRepository cargoowner, LogisticsDbContext _db)
        {
            _cargoownerRepository=cargoownerRepository;
            _cargoowner = cargoowner;
            db=_db;
        }
        public async Task<CargoownerDto> Create(CargoownerDto ca)
        {
            var carg = new Cargoowner
            {
                NumberId = ca.NumberId,
                ContractTitle = ca.ContractTitle,
                OwnerUnit = ca.OwnerUnit,
                PersoninCharge = ca.PersoninCharge,
                Line = ca.Line,
                Freightrate = ca.Freightrate,
                CharterConditions = ca.CharterConditions,
                CharterAmount = ca.CharterAmount,
                SigningDate = ca.SigningDate,
                Handler = ca.Handler,
                ProjectDescription = ca.ProjectDescription,
                ChangeClause = ca.ChangeClause,
                Enclosure = ca.Enclosure
            };

            await _cargoowner.Create(carg);
            return ObjectMapper.Map<Cargoowner, CargoownerDto>(carg,ca);
        }

        public async Task<int> Delete(int Id)
        {
            var result = await _cargoowner.Delete(Id);
            return result;
        }

        public async Task<CargoownerDto> GetCargoownerById(int Id)
        {
            var student = await _cargoowner.GetCargoownerById(Id);
            return ObjectMapper.Map<Cargoowner, CargoownerDto>(student);
        }

        public async Task<PagedResultDto<CargoownerDto>> GetCargoowners(CargoownerSearchDto csd)
        {
            var carryList = await _cargoownerRepository.GetPagedListAsync(
                csd.SkipCount,
                csd.MaxResultCount,
                csd.Sorting);
            if (!string.IsNullOrEmpty(csd.Title))
            {
                carryList.Where(m => m.ContractTitle.Contains(csd.Title)).ToList();
            }
            if (!string.IsNullOrEmpty(csd.Time))
            {
                carryList.Where(m => m.CreateTimedate == (Convert.ToDateTime(csd.Time))).ToList();
            }
            var totalCount = await AsyncExecuter.CountAsync(
                 _cargoownerRepository.WhereIf(
                     !csd.Title.IsNullOrWhiteSpace(),
                     m => m.ContractTitle.Contains(csd.Title)
                 )
             );

            return new PagedResultDto<CargoownerDto>(
              totalCount,
              ObjectMapper.Map<List<Cargoowner>, List<CargoownerDto>>(carryList)
          );
        }

        public async Task<PagedResultDto<CargoownerDto>> GetDapperCargoowners(CargoownerSearchDto csd)
        {
            var carr = db.Cargoowner.ToList();
            var file = db.Field.ToList();
            var lin = db.Line.ToList();
            var own = db.Owner.ToList();
            var list = from a in carr
                       join b in own on a.OwnerUnit equals b.Id
                       join c in file on a.PersoninCharge equals c.Id
                       join d in lin on a.Line equals d.Id
                       select new CargoownerDto
                       {
                           Id = a.Id,
                           NumberId = a.NumberId,
                           ContractTitle = a.ContractTitle,
                           OwnerUnit = a.OwnerUnit,
                           PersoninCharge = a.PersoninCharge,
                           Line = a.Line,
                           Freightrate = a.Freightrate,
                           CharterConditions = a.CharterConditions,
                           CharterAmount = a.CharterAmount,
                           SigningDate = a.SigningDate,
                           Handler = a.Handler,
                           State=a.State,
                           ProjectDescription = a.ProjectDescription,
                           ChangeClause = a.ChangeClause,
                           Enclosure = a.Enclosure,
                           CreateTimedate = a.CreateTimedate,
                           OName = b.Name,
                           FName = c.Name,
                           LName = d.Name,
                       };
            if (!string.IsNullOrEmpty(csd.Title))
            {
                list = list.Where(m => m.ContractTitle.Contains(csd.Title));
            }
            var totalCount = list.Count();
            list = list.Skip(csd.SkipCount).Take(csd.MaxResultCount);
            return new PagedResultDto<CargoownerDto>(
              totalCount,
              list.ToList()
          );
        }

        public async Task<int> Update(CargoownerDto ca)
        {
            var carr = await _cargoownerRepository.GetAsync(ca.Id);
            carr.ContractTitle = ca.ContractTitle;
            carr.OwnerUnit = ca.OwnerUnit;
            carr.PersoninCharge = ca.PersoninCharge;
            carr.Line = ca.Line;
            carr.Freightrate = ca.Freightrate;
            carr.CharterConditions = ca.CharterConditions;
            carr.CharterAmount = ca.CharterAmount;
            carr.SigningDate = Convert.ToDateTime(ca.SigningDate);
            carr.Handler = ca.Handler;
            carr.ProjectDescription = ca.ProjectDescription;
            carr.ChangeClause = ca.ChangeClause;
            carr.Enclosure = ca.Enclosure;
            await _cargoownerRepository.UpdateAsync(carr);
            return 1;
        }
    }
}
